/* $Id: UtilityDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.UtilityDAO;
import ae.pcfc.etks.online.common.domain.TTscCity;
import ae.pcfc.etks.online.common.domain.TTscClientRole;
import ae.pcfc.etks.online.common.domain.TTscClientRoleToDepartmentMap;
import ae.pcfc.etks.online.common.domain.TTscCountry;
import ae.pcfc.etks.online.common.domain.TTscDepartments;
import ae.pcfc.etks.online.common.domain.TTscEmirate;
import ae.pcfc.etks.online.common.domain.TTscEmirateCity;
import ae.pcfc.etks.online.common.domain.TTscLicIssuingAuthority;
import ae.pcfc.etks.online.common.domain.TTscLicType;
import ae.pcfc.etks.online.common.domain.TTscNametitle;
import ae.pcfc.etks.online.common.domain.TTscVisaType;

@Repository
public class UtilityDAOImpl extends SingleClientBaseDAO implements UtilityDAO {

	/**
	 * getCountryList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getCountryList(java.lang.String)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscCountry> getCountryList(String locale) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscCountry.class);

		if (locale != null && locale.equals("ar")) {
			criteria.addOrder(Order.asc("countryNameArb"));
		} else {
			criteria.addOrder(Order.asc("countryName"));
		}

		return criteria.list();
	}
	
	/**
	 * getCountryByCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getCountryByCode(java.lang.String)
	 * 
	 */
	public TTscCountry getCountryByCode(String countryCode){
		
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(TTscCountry.class);
		criteria.add(Restrictions.eq("countryCode", countryCode));

		return (TTscCountry) criteria.uniqueResult();
	}

	/**
	 * getClientRoleList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getClientRoleList(java.lang.String,
	 *      boolean)
	 * 
	 */
	/**
	 * getClientRoleList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getClientRoleList(java.lang.String, boolean)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscClientRole> getClientRoleList(String clientCategory,
			boolean bHideDefault) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscClientRole.class);
		if (clientCategory != null)
			criteria.add(Restrictions.eq("crCategory", clientCategory));
		if (bHideDefault) {
			criteria.add(Restrictions.ne("crId",
					ConstantsCommon.DEFAULT_SERVICE_ROLE_COMPANY_ID));
		}
		criteria.add(Restrictions.eq("crStatus", ConstantsCommon.ACTIVE));
		List<TTscClientRole> clientRoles = criteria.list();

		return (clientRoles != null && clientRoles.size() > 0) ? clientRoles
				: null;

	}

	/**
	 * getCityListByCountryCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getCityListByCountryCode(java.lang.String,
	 *      java.lang.String)
	 * 
	 */
	/**
	 * getCityListByCountryCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getCityListByCountryCode(java.lang.String, java.lang.String)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscCity> getCityListByCountryCode(String countryCode,
			String locale) {
		Criteria city = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscCity.class, "city");
		city.add(Restrictions.eq("city.cityCountryCode", countryCode));

		if (locale != null && locale.equals("ar"))
			city.addOrder(Order.asc("cityNameArb"));
		else
			city.addOrder(Order.asc("cityName"));

		return city.list();
	}
	
	/**
	 * getCityByCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getCityByCode(java.lang.String)
	 * 
	 */
	public TTscCity getCityByCode(String cityCode){
		
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(TTscCity.class);
		
		criteria.add(Restrictions.eq("cityCode", cityCode));
		criteria.add(Restrictions.eq("cityStatus", ConstantsCommon.ACTIVE));
		return (TTscCity) criteria.uniqueResult();

	}

	/**
	 * getLicIssuingAuth
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getLicIssuingAuth()
	 * 
	 */
	/**
	 * getLicIssuingAuth
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getLicIssuingAuth()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscLicIssuingAuthority> getLicIssuingAuth() {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscLicIssuingAuthority.class);
		criteria.add(Restrictions.eq("iaStatus", ConstantsCommon.ACTIVE));
		criteria.add(Restrictions.eq("iaAllowAdd", ConstantsCommon.ENABLE));
		
		return criteria.addOrder( Order.asc("iaDesc") ).list();
	}

	/**
	 * getLicType
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getLicType()
	 * 
	 */
	/**
	 * getLicType
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getLicType()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscLicType> getLicType() {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscLicType.class);
		criteria.add(Restrictions.eq("ltStatus", ConstantsCommon.ACTIVE));
		return criteria.addOrder( Order.asc("ltDesc") ).list();
	}

	/**
	 * getNameTitle
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getNameTitle()
	 * 
	 */
	/**
	 * getNameTitle
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getNameTitle()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscNametitle> getNameTitle() {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscNametitle.class);
		criteria.add(Restrictions.eq("ntStatus", ConstantsCommon.ACTIVE));
		return criteria.addOrder( Order.asc("ntDesc") ).list();
		
	}

	/**
	 * getNameTitleById
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getNameTitleById(java.lang.Long)
	 * 
	 */
	/**
	 * getNameTitleById
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getNameTitleById(java.lang.Long)
	 * 
	 */
	public TTscNametitle getNameTitleById(Long nameTitleId) {

		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(TTscNametitle.class);
		criteria.add(Restrictions.eq("ntId", nameTitleId.byteValue()));
		criteria.add(Restrictions.eq("ntStatus", ConstantsCommon.ACTIVE));
		return (TTscNametitle) criteria.uniqueResult();

	}

	/**
	 * getVisaType
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getVisaType()
	 * 
	 */
	/**
	 * getVisaType
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getVisaType()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscVisaType> getVisaType() {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscVisaType.class);
		criteria.add(Restrictions.eq("vtStatus", ConstantsCommon.ACTIVE));
		return criteria.addOrder( Order.desc("vtDesc") ).list();
	}
	
	/**
	 * getVisaTypeByCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getVisaTypeByCode(java.lang.String)
	 * 
	 */
	public TTscVisaType getVisaTypeByCode(String visaCode){
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscVisaType.class);
		criteria.add(Restrictions.eq("vtCode", visaCode));
		criteria.add(Restrictions.eq("vtStatus", ConstantsCommon.ACTIVE));
		return (TTscVisaType) criteria.uniqueResult(); 
	}

	/**
	 * getDepartments
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getDepartments()
	 * 
	 */
	/**
	 * getDepartments
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getDepartments()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscDepartments> getDepartments() {
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(TTscDepartments.class)
				.addOrder(Order.asc("deptCode")).list();
	}

	/**
	 * getAvailableClientRoleList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getAvailableClientRoleList(java.lang.String,
	 *      java.util.List)
	 * 
	 */
	/**
	 * getAvailableClientRoleList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getAvailableClientRoleList(java.lang.String, java.util.List)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscClientRole> getAvailableClientRoleList(
			String clientCategory, List<String> roleList) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscClientRole.class);
		criteria.add(Restrictions.eq("crCategory", clientCategory));
		if (roleList != null && roleList.size() > 0) {
			criteria.add(Restrictions.not(Restrictions.in("crId", roleList)));
		}
		criteria.add(Restrictions.eq("crStatus", ConstantsCommon.ACTIVE));
		List<TTscClientRole> clientRoles = criteria.list();

		return (clientRoles != null && clientRoles.size() > 0) ? clientRoles
				: null;
	}
	
	/**
	 * getLicIssuingAuthorityByCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getLicIssuingAuthorityByCode(java.lang.String)
	 * 
	 */
	public TTscLicIssuingAuthority getLicIssuingAuthorityByCode(String licIACode){
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscLicIssuingAuthority.class);
		
		criteria.add(Restrictions.eq("iaCode", licIACode));
		criteria.add(Restrictions.eq("iaStatus", ConstantsCommon.ACTIVE));
		TTscLicIssuingAuthority issuingAuthority = (TTscLicIssuingAuthority) criteria.uniqueResult();
		
		return issuingAuthority;
	}

	/**
	 * findServiceDepartmentsByDepartmentId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#findServiceDepartmentsByDepartmentId(java.util.List)
	 * 
	 */
	public List<TTscDepartments> findServiceDepartmentsByDepartmentId(List<String> departmentCodes) {
		// TODO Auto-generated method stub
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscDepartments.class);
		if(departmentCodes != null && departmentCodes.size() > 0){
			List<Long> lDeptList = new ArrayList<Long>();
			for (String code : departmentCodes) {
				lDeptList.add(Long.valueOf(code));
			}
			criteria.add(Restrictions.in("deptId", lDeptList));
		}
		@SuppressWarnings("unchecked")
		List<TTscDepartments> departments = criteria.list();

		return (departments != null && departments.size() > 0) ? departments
				: null;

	}

	/**
	 * getDepartmentById
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getDepartmentById(java.lang.String)
	 * 
	 */
	public TTscDepartments getDepartmentById(String serviceDefaultDeptCode) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscDepartments.class);

		criteria.add(Restrictions.eq("deptId",
				Long.valueOf(serviceDefaultDeptCode)));

		@SuppressWarnings("unchecked")
		List<TTscDepartments> departments = criteria.list();

		return (departments != null && departments.size() > 0) ? departments
				.get(0) : null;
	}

	/**
	 * getDepartmentsByRoleIds
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getDepartmentsByRoleIds(java.util.List)
	 * 
	 */
	public List<TTscClientRoleToDepartmentMap> getDepartmentsByRoleIds(List<String> roleIds) {
		// TODO Auto-generated method stub
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscClientRoleToDepartmentMap.class);
		
		if(roleIds != null && roleIds.size() > 0){
			criteria.add(Restrictions.in("clientRoleId", roleIds));
		} else {
			return null;
		}
		criteria.add(Restrictions.eq("status", ConstantsCommon.ACTIVE));

		@SuppressWarnings("unchecked")
		List<TTscClientRoleToDepartmentMap> departments = criteria.list();

		return (departments != null && departments.size() > 0) ? departments
				: null;
	}

	/**
	 * getDepartmentByCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getDepartmentByCode(java.lang.String)
	 * 
	 */
	public TTscDepartments getDepartmentByCode(String strDeptCode) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscDepartments.class);

		criteria.add(Restrictions.eq("deptCode", strDeptCode));

		@SuppressWarnings("unchecked")
		List<TTscDepartments> departments = criteria.list();

		return (departments != null && departments.size() > 0) ? departments
				.get(0) : null;
	}

	/**
	 * getAllLicIssuingAuth
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UtilityDAO#getAllLicIssuingAuth()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<TTscLicIssuingAuthority> getAllLicIssuingAuth() {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(TTscLicIssuingAuthority.class);
		criteria.add(Restrictions.eq("iaStatus", ConstantsCommon.ACTIVE));

		return criteria.addOrder(Order.asc("iaDesc")).list();
	}
	
	
	/**
	 * 
	 * 
	 * @see UtilityDAOImpl
	 */
	public List<TTscEmirate> getEmirates(String locale) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(TTscEmirate.class);

		if (locale != null && locale.equals("ar")) {
			criteria.addOrder(Order.asc("emirateNameArb"));
		} else {
			criteria.addOrder(Order.asc("emirateName"));
		}

		return criteria.list();
	}
	
	
	/**
	 * 
	 * @see UtilityDAOImpl
	 */
	public List<TTscEmirateCity> getEmirateCityByEmirateCode(String emirateCode,
			String locale) {
		
		Criteria city = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(TTscEmirateCity.class, "emirateCity");
		city.add(Restrictions.eq("emirateCity.emirateCode", emirateCode));

		if (locale != null && locale.equals("ar"))
			city.addOrder(Order.asc("emirateCityNameArb"));
		else
			city.addOrder(Order.asc("emirateCityName"));
		
		return city.list();
	}
}
